/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.account.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.model.CompanyCardDetailVo;
import com.rzico.account.model.CompanyCardVo;
import com.rzico.account.model.EmployeeCardVo;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Card;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.account.model.CardExtVo;
import com.rzico.basics.service.CardService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.account.entity.CardBill;
import com.rzico.account.service.CardBillService;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 会员卡账单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "会员卡账单接口")
@RestController
@RequestMapping("/admin/card_bill")
public class CardBillAdminController extends BaseController {

    @Autowired
    private CardBillService cardBillService;

    @Autowired @Lazy
    private CardService cardService;

    @Autowired @Lazy
    private SysSequenceService sysSequenceService;

    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 分页查询会员卡账单
     *
     * @return
     */
    @ApiOperation("分页查询会员卡账单")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "cardId", value = "会员卡id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "month", value = "月份", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "会员id", dataType = "String", paramType = "query"),
    })
    public CommResult<CardBill> list(Long cardId,Long memberId,String month, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);

        if (memberId!=null) {
            params.put("memberId",memberId);
        }

        params.put("cardId", cardId);
        if (month!=null) {
            String m = month.concat("-01");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(m);
                d = DateUtils.addMonths(d, 1);
                d = DateUtils.addMilliseconds(d, -1);
                params.put("endDate", d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CardBill> list = cardBillService.selectList(params);
        PageResult<CardBill> pageResult = new PageResult<CardBill>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 企业员工充值
     *
     * @return
     */
    @ApiOperation("企业员工充值")
    @PostMapping("/uploadChargeMoney")
    public CommResult<CardExtVo> uploadChargeMoney(@RequestBody List<CardExtVo> list, Long companyId, Integer cardType, String memo,
                                                   String startDate, String endDate, Long[] tagIds){
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String,Object> result = cardBillService.checkUploadInfo(list, enterprise.getId(), companyId, cardType, memo, startDate, endDate);
        if (result.get("returnCode").equals("success")) {
            String code = sysSequenceService.generate("card");
            List<CardExtVo> resultList = cardBillService.upload(list, enterprise.getId(), companyId, cardType, code, memo, startDate, endDate, tagIds, enterprise.getMchId());
        }

        return CommResult.success(result);
    }

    /**
     * 获取企业的福利金余额
     *
     * @return
     */
    @ApiOperation("获取企业的福利金余额")
    @GetMapping("/getCompanyBalance/{companyId}")
    public CommResult<Card> getCompanyBalance(@PathVariable String companyId) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companyId", companyId);
        params.put("enterpriseId", enterprise.getId());
        params.put("cardType", 1);
        params.put("status", 1);
        java.math.BigDecimal balance = cardBillService.getCompanyBalance(params);
        return CommResult.success(balance);
    }

    /**
     * 获取企业的所发放的批次信息
     *
     * @return
     */
    @ApiOperation("获取企业的所发放的批次信息")
    @PostMapping("/selectCompanyAllCard")
    public CommResult<CompanyCardVo> selectCompanyAllCard(@RequestBody CompanyCardVo companyCard, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companyId", companyCard.getCompanyId());
        params.put("enterpriseId", enterprise.getId());
        if (null != companyCard.getCreateYear() && StringUtils.isNotEmpty(companyCard.getCreateYear())) {
            params.put("createYear", companyCard.getCreateYear());
        }
        if (null != companyCard.getMemo() && StringUtils.isNotEmpty(companyCard.getMemo())) {
            params.put("memo", companyCard.getMemo());
        }
        if (null != companyCard.getBatchNum() && StringUtils.isNotEmpty(companyCard.getBatchNum())){
            params.put("batchNum", companyCard.getBatchNum());
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CompanyCardVo> result = cardBillService.selectCompanyAllCard(params);
        PageResult<CompanyCardVo> pageResult = new PageResult<CompanyCardVo>(result, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 获取企业的所发放的福利金详细信息
     *
     * @return
     */
    @ApiOperation("获取企业的所发放的福利金详细信息")
    @PostMapping("/selectCompanyCardDetail")
    public CommResult<CompanyCardDetailVo> selectCompanyCardDetail(@RequestBody CompanyCardDetailVo companyCardDetail, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("companyId", companyCardDetail.getCompanyId());
        if (null != companyCardDetail.getCreateYear() && StringUtils.isNotEmpty(companyCardDetail.getCreateYear())) {
            params.put("createYear", companyCardDetail.getCreateYear());
        }
        if (null != companyCardDetail.getMemo() && StringUtils.isNotEmpty(companyCardDetail.getMemo())) {
            params.put("memo", companyCardDetail.getMemo());
        }
        if (null != companyCardDetail.getBatchNum() && StringUtils.isNotEmpty(companyCardDetail.getBatchNum())){
            params.put("batchNum", companyCardDetail.getBatchNum());
        }
        params.put("enterpriseId", enterprise.getId());
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CompanyCardDetailVo> result = cardBillService.selectCompanyCardDetail(params);
        PageResult<CompanyCardDetailVo> pageResult = new PageResult<CompanyCardDetailVo>(result, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 获取企业员工得福利卡信息
     *
     * @return
     */
    @ApiOperation("获取企业员工得福利卡信息")
    @PostMapping("/selectCompanyEmployeeCard")
    public CommResult<EmployeeCardVo> selectCompanyEmployeeCard(@RequestBody EmployeeCardVo employeeCard, Pageable pageable) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("memberId", employeeCard.getMemberId());
        if (null != employeeCard.getCompanyId()) {
            params.put("companyId", employeeCard.getCompanyId());
        }
        params.put("enterpriseId", enterprise.getId());
        if (null != employeeCard.getMemo() && StringUtils.isNotEmpty(employeeCard.getMemo())) {
            params.put("memo", employeeCard.getMemo());
        }
        if (null != employeeCard.getCreateTime() && StringUtils.isNotEmpty(employeeCard.getCreateTime())) {
            params.put("createTime", employeeCard.getCreateTime());
        }
        if (null != employeeCard.getCardType()) {
            params.put("cardType", employeeCard.getCardType());
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EmployeeCardVo> result = cardBillService.selectCompanyEmployeeCard(params);
        PageResult<EmployeeCardVo> pageResult = new PageResult<EmployeeCardVo>(result, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 删除福利金卡及卡充值信息
     *
     * @param codeBatch
     * @return
     */
    @ApiOperation("删除福利金卡及卡充值信息")
    @PostMapping("/delCompanyCard/{codeBatch}")
    public CommResult<Card> delCompanyCard(@PathVariable String codeBatch) {
        Enterprise enterprise = enterpriseService.getCurrent();
        if (null == enterprise) {
            return CommResult.error("没有开通企业");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("code", codeBatch + "-");
        params.put("enterpriseId", enterprise.getId());
        int result = cardBillService.deleteCardAndBillById(params);
        return CommResult.success(result);
    }

}
